package com.dorje.sys.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.dorje.sys.service.BaseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

public class BaseController<T> {
	
	@Autowired
	BaseService<T> baseService;
	
	@RequestMapping("list")
	public void list(@ModelAttribute T t,HttpServletRequest request,HttpServletResponse response) throws IOException{
		Integer pageNum = 1;
		Integer rows = 10;
		try {
			pageNum = Integer.valueOf(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		Page<T> page = PageHelper.startPage(pageNum, rows);
		List<T> ts = baseService.findAll( t);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", ts);
		map.put("total", page.getTotal());
		String str = JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd HH:mm:ss.SSS");
		response.getWriter().write(str);
	}
	
}
